﻿namespace System.Collections.Generic
{
    static public class DictionaryExtention
    {
        #region Static public methods

        static public TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
            where TValue : new()
        {
            TValue value = dictionary.TryGetValue(key);

            if (value == null)
            {
                value = new TValue();
                dictionary.Add(key, value);
            }

            return value;
        }

        static public TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
        {
            TValue value;
            dictionary.TryGetValue(key, out value);
            return value;
        }

        #endregion
    }
}